train = find(imdb.images.set == 1 & imdb.images.segmentation) ;
N=2;

% Class statistics
classCounts = zeros(N,1) ;
for i = 1:numel(train)
  fprintf('%s: computing segmentation stats for training image %d\n', mfilename, i) ;
  lb = imread(sprintf(imdb.paths.classSegmentation, imdb.images.name{train(i)})) ;
  lb(lb<255&lb>0)=255;lb=lb/255;
  classCounts = classCounts + accumarray(lb(ok(:))+1, 1, [N 1]) ;
end
classCounts = classCounts ;

rgbm1={};rgbm2={};
% Image statistics
for t=1:numel(train)
  fprintf('%s: computing RGB stats for training image %d\n', mfilename, t) ;
  rgb = imread(sprintf(imdb.paths.image, imdb.images.name{train(t)})) ;
  rgb = single(rgb) ;
  z = reshape(permute(rgb,[3 1 2 4]),1,[]) ;
  n = size(z,2) ;
  rgbm1{t} = sum(z,2)/n ;
  rgbm2{t} = z*z'/n ;
end
rgbm1 = mean(cat(2,rgbm1{:}),2) ;
rgbm2 = mean(cat(3,rgbm2{:}),3) ;

rgbMean = [rgbm1;rgbm1;rgbm1] ;
rgbCovariance = [rgbm2 - rgbm1*rgbm1', rgbm2 - rgbm1*rgbm1' , rgbm2 - rgbm1*rgbm1' ];

save('imdbStats.mat','classCounts', 'rgbMean', 'rgbCovariance');